package com.atguigu.gmall.product.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Service
public class TestServiceImpl implements TestService {

    @Autowired
    private RedisTemplate redisTemplate;

    private final Lock lock = new ReentrantLock();

    @Override
    public void testLock() {

//        this.lock.lock();
        Boolean lock = redisTemplate.opsForValue().setIfAbsent("lock", 0);
        if (lock) {

            Integer num = (Integer) redisTemplate.opsForValue().get("num");

            num++;

            redisTemplate.opsForValue().set("num", num);

            redisTemplate.delete("lock");
            /*finally {
                this.lock.unlock();
            }*/
        }else {
            testLock();
        }

    }
}
